package org.linlinjava.litemall.db.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.db.dao.LitemallGiftCardClassificationMapper;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallGiftCard;
import org.linlinjava.litemall.db.domain.LitemallGiftCardClassification;
import org.linlinjava.litemall.db.util.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author ：ply
 * @date ：Created in 2024/4/26 11:50
 * @description：礼物卡分类
 */
@Service
public class LitemallGiftCardClassificationService extends MPJBaseServiceImpl<LitemallGiftCardClassificationMapper, LitemallGiftCardClassification> {

    @Resource
    private LitemallGiftCardClassificationMapper litemallGiftCardClassificationMapper;

    public IPage<LitemallGiftCardClassification> querySelective(LitemallGiftCardClassification litemallGiftCardClassification, Integer page, Integer limit, String sort, String order) {
        LambdaQueryWrapper<LitemallGiftCardClassification> queryWrapper = new LambdaQueryWrapper<>();

        Optional.ofNullable(litemallGiftCardClassification.getClassificationName()).filter(StringUtils::isNotEmpty)
                .ifPresent(name -> queryWrapper.like(LitemallGiftCardClassification::getClassificationName, name));

        Optional.ofNullable(litemallGiftCardClassification.getClassificationStatus())
                .ifPresent(status -> queryWrapper.eq(LitemallGiftCardClassification::getClassificationStatus, status));

        queryWrapper.orderBy(StringUtils.isNotEmpty(sort), "desc".equalsIgnoreCase(order), LitemallGiftCardClassification::getClassificationSort);
        return litemallGiftCardClassificationMapper.selectPage(new Page<>(page,limit),queryWrapper);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    public LitemallGiftCardClassification findById(Integer id) {
        return litemallGiftCardClassificationMapper.selectById(id);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(Integer id) {
        litemallGiftCardClassificationMapper.deleteById(id);
    }

    /**
     * 新增
     * @param litemallGiftCardClassification
     */
    public void add(LitemallGiftCardClassification litemallGiftCardClassification) {
        litemallGiftCardClassification.setAddTime(LocalDateTime.now());
        litemallGiftCardClassification.setUpdateTime(LocalDateTime.now());
        litemallGiftCardClassificationMapper.insert(litemallGiftCardClassification);
    }

    /**
     * 修改
     * @param litemallGiftCardClassification
     */
    public void update(LitemallGiftCardClassification litemallGiftCardClassification) {
        litemallGiftCardClassification.setUpdateTime(LocalDateTime.now());
        litemallGiftCardClassificationMapper.updateById(litemallGiftCardClassification);
    }


    public Map<Integer, LitemallGiftCardClassification> getClassificationMap(List<Integer> ids) {
        if(CollUtil.isEmpty(ids)) return CollUtil.newHashMap();
        return CollectionUtils.convertMap(litemallGiftCardClassificationMapper.selectBatchIds(ids), LitemallGiftCardClassification::getId);
    }
}
